<?php

namespace App\Admin\Actions\ShopOrderReturn;

use App\Library\Logic\OpenIdLogic;
use App\Library\Logic\ShopOrderExtLogic;
use App\Library\Logic\ShopOrderLogic;
use App\Library\Service\WxMessageService;
use App\Models\ShopOrderReturnModel;
use Encore\Admin\Actions\RowAction;
use Encore\Admin\Facades\Admin;
use Illuminate\Database\Eloquent\Model;

class ShopOrderReturnAuction extends RowAction
{
    /**
     * onlySaveModel
     * @param ShopOrderReturnModel $model
     * @param int $status
     * @return void
     */
    protected function saveModel(ShopOrderReturnModel $model, int $status = 5): void
    {
        $model->or_sell_status = $status;
        $model->or_buy_status = $status;
        $model->or_approved_time = time();
        $model->edit_user_id = Admin::user()->user_id;
        ShopOrderExtLogic::updateByWhere(['statusAfterSale' => 2, 'status' => $status], 'orderId', $model->order_id);
        ShopOrderLogic::updateByWhere(['orderBuyStatus' => $status, 'orderSellStatus' => $status], 'orderId', $model->order_id);
    }

    public static function salesHandle(Model $model, $remark = ''): bool
    {
        $userInfo = OpenIdLogic::query()->where('user_id', $model->buy_user_id)->first();
        if (!empty($userInfo)) {
            $orderInfo = ShopOrderLogic::query()
                ->where('order_id', $model->order_id)
                ->first();
            $orderData = [
                'goods_order_no' => $orderInfo->serial_number,
                'handling_result' => $remark,
                'time' => date('Y年m月d日 H:i', time()),
            ];
            WxMessageService::goodsWxMess($orderData, $userInfo, [], 4);
        }
        return true;
    }
}
